/*
 * Copyright (c) 2012 free soft workgroup
 * All rights reserved
 *
 */
#ifndef _SECURITY_H
#define _SECURITY_H

#include "stdnet.h"
/*
*des encryption and decryption
*/
struct des_context
{
    ulong esk[32];     /* DES encryption subkeys */
    ulong dsk[32];     /* DES decryption subkeys */
};

struct des3_context
{
    ulong esk[96];     /* Triple-DES encryption subkeys */
    ulong dsk[96];     /* Triple-DES decryption subkeys */
};

typedef struct _md5ctx {
	uint32_t state[4];
	uint32_t count[2];
	uint8_t buffer[64];
} md5ctx;

/*
 * DES key schedule
 */
void des_set_key( des_context *ctx, uchar key[8] );

/*
 * DES 64-bit block encryption (ECB)
 */
void des_encrypt( des_context *ctx, uchar input[8], uchar output[8] );

/*
 * DES 64-bit block decryption (ECB)
 */
void des_decrypt( des_context *ctx, uchar input[8], uchar output[8] );

/*
 * DES-CBC encryption
 */
void des_cbc_encrypt( des_context *ctx, uchar iv[8],
                      uchar *input, uchar *output, uint len );

/*
 * DES-CBC decryption
 */
void des_cbc_decrypt( des_context *ctx, uchar iv[8],
                      uchar *input, uchar *output, uint len );

/*
 * Triple-DES key schedule (112-bit)
 */
void des3_set_2keys( des3_context *ctx, uchar key[16] );

/*
 * Triple-DES key schedule (168-bit)
 */
void des3_set_3keys( des3_context *ctx, uchar key[24] );

/*
 * Triple-DES 64-bit block encryption (ECB)
 */
void des3_encrypt( des3_context *ctx, uchar input[8], uchar output[8] );

/*
 * Triple-DES 64-bit block decryption (ECB)
 */
void des3_decrypt( des3_context *ctx, uchar input[8], uchar output[8] );

/*
 * 3DES-CBC encryption
 */
void des3_cbc_encrypt( des3_context *ctx, uchar iv[8],
                       uchar *input, uchar *output, uint len );

/*
 * 3DES-CBC decryption
 */
void des3_cbc_decrypt( des3_context *ctx, uchar iv[8],
                       uchar *input, uchar *output, uint len );

/*
 * Triple-DES 64-bit block encrypt (ECB),use default key
 */
size_t des3_encrypt_default(uchar * input,size_t inlen, uchar * output,size_t outbuflen);

/*
 * Triple-DES 64-bit block decryption (ECB),use default key
 */
size_t des3_decrypt_default(uchar * input,size_t inlen, uchar * output,size_t outbuflen);


/**
 * md5 init function
 */
void md5_init(md5ctx *ctx);

/**
 * md5 update function
 */
void md5_update(md5ctx *ctx,const uint8_t *buff,uint32_t leng);

/**
 * md5 final function
 */
void md5_final(uint8_t digest[16],md5ctx *ctx);

/**
 * md5 hash sum
 * uint8_t dst[16]
 */
void md5_sum(uint8_t *dst, const uint8_t *src, const int len);

/*
 * Checkup routine
 */
int des_self_test( void );


#endif /*_SECURITY_H */
